<?php
/**
 * User: aka
 * Time: 2:40
 */

namespace app\read\controller;

use app\validate\riven\QueryRiven;
use think\facade\Session;
use think\Request;

class Riven
{
    private $userId;

    public function __construct()
    {
        $this->userId = Session::get('user_info.id');
    }

    public function __destruct()
    {
        unset($this->userId);
    }

    public function queryRiven(Request $request)
    {
        $validate = new QueryRiven();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else {
            $queryStuffName = $request->param('query_stuff_name');
            $rivenList = \app\tenno\model\Riven::deleteState(0)
                ->stuffName($queryStuffName)
                ->paginate(10)
                ->each(
                    function($item)
                    {
                        $item->uid = \app\tenno\model\Users::userInfo($item->uid)
                            ->find();
                    }
                );
            return view('query_riven', ['riven_list' => $rivenList, 'query_name' => $queryStuffName]);
        }
    }

    public function queryUserRiven(Request $request)
    {
        $validate = new QueryRiven();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else {
            $queryStuffName = $request->param('query_stuff_name');
            $rivenList = \app\tenno\model\Riven::deleteState(0)
                ->stuffName($queryStuffName)
                ->uid($this->userId)
                ->paginate(10);
            return view('query_user_riven', ['list' => $rivenList, 'query_name' => $queryStuffName]);
        }
    }
}